﻿using System;
using System.Collections.Generic;
using System.Linq;
using NineRays.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using TreeNode = NineRays.WebControls.FlyTreeNode;
namespace CARMS.Admin
{
    [Function("Admin.AccessControl")]
    public partial class AccessControl : PageBase
    {
        private List<ActionEntity> CurrentActionList
        {
            get
            {
                if (ViewState["CurrentActionList"] == null)
                {
                    ViewState["CurrentActionList"] = AdminBusiness.GetActionList();
                }
                return (List<ActionEntity>)ViewState["CurrentActionList"];
            }
            set { ViewState["CurrentActionList"] = value; }
        }



        private List<string> CurrentSelectedActionList
        {
            get
            {
                if (ViewState["CurrentSelectedActionList"] == null)
                {
                    ViewState["CurrentSelectedActionList"] = new List<string>();
                }
                return (List<string>)ViewState["CurrentSelectedActionList"];
            }
            set { ViewState["CurrentSelectedActionList"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblMessage.Text = "";
                ControlHelper.BindRoleList(listRoles, EmptyType.None);

                BindActionList();
            }
        }

        private void BindActionList()
        {


            if (listRoles.Items.Count > 0)
            {
                if (listRoles.SelectedItem == null)
                {
                    listRoles.Items[0].Selected = true;
                }
                if (tvActions.Nodes.Count > 0)
                {
                    tvActions.Nodes.Clear();
                }
                LoadActions(null, null);

                var itemCollection = new RoleActionCollection();
                itemCollection.GetMulti(RoleActionFields.RoleId == listRoles.SelectedItem.Value);
                foreach (var item in itemCollection)
                {
                    var node = tvActions.FindByValue(item.ActionId);
                    if (node != null)
                    {
                        node.Checked = true;
                    }
                }
            }
        }
        private void LoadActions(TreeNode parentNode, ActionEntity module)
        {
            var result = from entity in CurrentActionList where entity.ParentId == (module == null ? string.Empty : module.ActionId) orderby entity.SortIndex select entity;

            var children = result.ToList<ActionEntity>();
            for (int i = 0; i < children.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = children[i].ActionName + "(" + children[i].Description + ")";
                node.Value = children[i].ActionId;
                node.IsCheckbox = true;

                if (parentNode == null)
                {
                    tvActions.Nodes.Add(node);

                }
                else
                {
                    parentNode.ChildNodes.Add(node);

                }

                LoadActions(node, children[i]);
            }
        }


        protected void listRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listRoles.SelectedItem != null)
            {
                BindActionList();
            }

        }
        protected void btnSaveRole_Click(object sender, EventArgs e)
        {
            ControlHelper.BindRoleList(listRoles, EmptyType.None);
        }
        protected void btnSaveActionToRole_Click(object sender, EventArgs e)
        {
            if (listRoles.SelectedItem == null)
            {
                base.Alert("Please select a role first.");
                return;
            }
            var roleID =int.Parse(listRoles.SelectedValue);
            CurrentSelectedActionList.Clear();
            GetWholeTreeValue(null);

            if (AdminBusiness.SaveActionToRole(roleID, CurrentSelectedActionList))
            {
                base.Alert("Successfully saved!");
            }
            else
            {
                base.Alert("Save failed!");
            }
        }

        private void GetWholeTreeValue(TreeNode parentNode)
        {
            if (parentNode == null)
            {
                foreach (var node in tvActions.Nodes)
                {
                    if (node.Checked)
                    {
                        CurrentSelectedActionList.Add(node.Value);
                        GetWholeTreeValue(node);
                    }
                }
            }
            else
            {
                if (parentNode.ChildNodes.Count > 0)
                {
                    foreach (var node in parentNode.ChildNodes)
                    {
                        if (node.Checked)
                        {
                            CurrentSelectedActionList.Add(node.Value);
                            GetWholeTreeValue(node);
                        }
                    }
                }
            }
        }
    }
}
